//package com.lyubia.shop.search.service.impl;
//
//import com.lyubia.shop.api.service.SearchService;
//import com.lyubia.shop.api.vo.GoodsVo;
//import com.lyubia.shop.common.result.ShopPageInfo;
//import org.apache.dubbo.config.annotation.Service;
//import org.elasticsearch.index.query.QueryBuilders;
//import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
//import org.springframework.data.domain.PageRequest;
//import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
//import org.springframework.data.elasticsearch.core.SearchHit;
//import org.springframework.data.elasticsearch.core.SearchHits;
//import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
//import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
//
//import javax.annotation.Resource;
//import java.util.ArrayList;
//import java.util.List;
//
//@Service(version = "1.0")
//public class SearchServiceImpl implements SearchService {
//
//    @Resource
//    private ElasticsearchRestTemplate elasticsearchRestTemplate;
//
//    @Override
//    public ShopPageInfo<GoodsVo> doSearch(String searchKey, Integer pageNum, Integer pageSize) {
//
//        NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder();
//
//        NativeSearchQuery query = nativeSearchQueryBuilder
//                .withQuery(QueryBuilders.multiMatchQuery(searchKey, "goodsName"))
//                .withPageable(PageRequest.of(pageNum, pageSize))
//                .withHighlightBuilder(new HighlightBuilder()
//                        .field("goodsName")
//                        .preTags("<span style='color:red;'>")
//                        .postTags("</span>"))
//                .build();
//        SearchHits<GoodsVo> search = elasticsearchRestTemplate.search(query, GoodsVo.class);
//
//        List<GoodsVo> goodsVoList = new ArrayList<>();
//        for (SearchHit<GoodsVo> searchHit : search) {
//            // 高亮对象
//            String highlightMessage = searchHit.getHighlightField("goodsName").get(0);
//            System.out.println(highlightMessage);
//            GoodsVo goodsVo = searchHit.getContent();
//            goodsVoList.add(goodsVo);
//        }
//        ShopPageInfo<GoodsVo> pageInfo = new ShopPageInfo<>();
//        pageInfo.setResult(goodsVoList);
//        return pageInfo;
//    }
//}
